function closeLayer() {
    //var index = parent.layer.getFrameIndex(window.name);
    //parent.layer.close(index);
    parent.layer.closeAll();
}

var url = '';
function initLayer() {
    //点取消关闭弹窗
    $('.cancel').click(function () {
        closeLayer();
    });

    //默认第一个部门选中
    $(function () {
        $('.people_left ul li:first').addClass('pe');
    })

    //点击部门获取人员
    $('.people_left ul li').on('click', function () {
        url = $(this).children().attr('data-url');
        // url = /index.php/Index/Index/getAjaxPage/dept_id/160/flow_id/163/run_id/1357/prcs_id/2/auto_type/9.html
        var id = $('input[name="flow_id"]').val();
        $(this).addClass('pe').siblings().removeClass('pe');
        getData(1);
    })

//    $('.people_left ul li').on('click', function () {
//        var url = $(this).children().attr('data-url');
//        var id = $('input[name="flow_id"]').val();
//        $(this).addClass('pe').siblings().removeClass('pe');
//        $.get(url, function (res) {
//            console.log(res);
//            if (res[0]) {
//                var html = '';
//                for (var i = 0; i < res.length; i++) {
//                    if (res[0]['checked'] && res.length == 1) {
//                        html += '<li class="pointer" data-id="' + res[i].user_id + '">';
//                        if (id != 0) {
//                            html += '<label class="checked"><img src="/Public/Index/img/s1.png" class="c-img" style="width:20px">主办</label> <span class="c-name checked">' + res[i].user_name + '</span></li>';
//                        } else {
//                            html += '<span class="c-name checked">' + res[i].user_name + '</span></li>';
//                        }
//                    } else if (res[0]['checked']) {
//                        html += '<li class="pointer" data-id="' + res[i].user_id + '">';
//                        if (id != 0) {
//                            html += '<label><img src="/Public/Index/img/s2.png" class="c-img" style="width:20px">主办</label> <span class="c-name checked">' + res[i].user_name + '</span></li>';
//                        } else {
//                            html += '<span class="c-name checked">' + res[i].user_name + '</span></li>';
//                        }
//                    } else {
//                        html += '<li class="pointer" data-id="' + res[i].user_id + '">';
//                        if (id != 0) {
//                            html += '<label><img src="/Public/Index/img/s2.png" class="c-img" style="width:20px">主办</label> <span class="c-name">' + res[i].user_name + '</span></li>';
//                        } else {
//                            html += '<span class="c-name">' + res[i].user_name + '</span></li>';
//                        }
//                    }
//                }
//                $('.select_peo ul').html(html);
//                $('.page').remove();    //去掉分页，此处需要改为ajax分页
//            } else {
//                $('.select_peo ul').html('<li><span style="color:#bbb">没有符合条件的用户</span></li>');
//            }
//
//        }
//        )
//    })

    //全部选中
    $('.add-all').on('click', function () {
        var form_id = $('input[name=id]').val();
        var table_key = $('input[name="table_key"]').val();  //layer.html页面传过来的key
        $('.select_peo ul li .c-name').addClass('checked');

        var userId = new Array();
        var userName = new Array();
        $('.select_peo ul li').each(function () {
            userId.push($(this).attr('data-id'));
            userName.push($(this).children('span').text());
        })

        if (form_id != 0) {
            tabName = '.addUser_' + form_id;
            var html = '<span>';
            for (var i = 0; i < userId.length; i++) {
                html += '<span class="one-name"><a>' + userName[i] + '</a>';
                html += '<img class="pointer delOperator" data-id="' + userId[i] + '" src="/Public/Index/img/close_red.png" alt="close" width="12"/>';
                html += '<input type="hidden"';
                if (table_key != 0) {
                    html += 'name="' + table_key + '[]"';
                } else {
                    html += 'name="user_ids[]"';
                }
                html += 'value="' + userId[i] + '">';
                html += '<input type="hidden" name="user_names[]" value="' + userName[i] + '"></span>';
            }
            html += '</span>';
            parent.$(tabName).parent('.value').find('.textarea').html(html);
        } else {
            var html = '<p class="people main_user"><span class="one">主办人：</span></p>';
            html += '<p class="other_user"><span class="one">经办人：</span>';
            for (var i = 0; i < userId.length; i++) {
                html += '<span><a>' + userName[i] + '</a>';
                html += '<img class="pointer delOperator" data-id="' + userId[i] + '" src="/Public/Index/img/close_red.png" alt="close" width="12" />';
                html += '<input type="hidden" name="user_ids[]" value="' + userId[i] + '">';
                html += '<input type="hidden" name="user_names[]" value="' + userName[i] + '"></span>';
            }
            html += '</p>';
            parent.$('.operator').html(html);
        }
    })

    //全部删除
    $('.add-del').on('click', function () {
        var form_id = $('input[name=id]').val();
        //$('.select_peo ul li').css({'background': 'none', 'color': '#000'});
        $('.select_peo ul li label,span').removeClass('checked');
        $('.select_peo ul li label img').attr('src', '/Public/Index/img/s2.png')
        if (form_id != 0) {
            tabName = '.addUser_' + form_id;
            parent.$(tabName).parent('.value').find('.textarea span:not(:first)').remove();
        } else {
            parent.$('.operator .main_user span:not(:first),.other_user span:not(:first)').remove();
        }
    })

    //选中经办人
    $('body').on('click', '.c-name', function () {
        var id = $(this).parent('li').attr('data-id');  //user_id
        var form_id = $('input[name="id"]').val();  //layer.html页面传过来的id
        var name = $(this).text();
        var table_key = $('input[name="table_key"]').val();  //layer.html页面传过来的key
        //alert(table_key)
        if ($(this).attr('class') == 'c-name') {
            $(this).addClass('checked');
            var html = '<span class="one-name"><a>' + name + '</a>';
            html += '<img class="pointer delOperator" data-id="' + id + '" src="/Public/Index/img/close_red.png" alt="close" width="12"/>';
            html += '<input type="hidden"';
            if (table_key != 0) {
                html += 'name="' + table_key + '[]"';
            } else {
                html += 'name="user_ids[]"';
            }

            html += 'value="' + id + '">';
            html += '<input type="hidden" name="user_names[]" value="' + name + '"></span>';
            if (form_id != 0) {
                //工作计划中的选择人员
                tabName = '.addUser_' + form_id;
//                alert(tabName);
                parent.$(tabName).parent('.value').find('.textarea span:last').after(html);
                parent.$(tabName).parent('.search').find('.textarea span:last').after(html);
            } else {
                //流程中的选择人员
                parent.$('.operator .other_user span:last').after(html);
            }

        } else {
            $(this).removeClass('checked');
            if (form_id != 0) {
                //工作计划中的选择人员
                parent.$(tabName).parent('.value').find('.textarea span').each(function () {
                    if (name == $(this).text()) {
                        $(this).remove();
                    }
                })
            } else {
                //流程中的选择人员
                parent.$('.operator .other_user span').each(function () {
                    if (name == $(this).text()) {
                        $(this).remove();
                    }
                })
            }

        }

    })

    //选中主办人
    $('body').on('click', '.c-img', function () {
        var id = $(this).parents('li').attr('data-id');
        var name = $(this).parent().next().text();
        if ($(this).attr('src') == '/Public/Index/img/s2.png') {
            $(this).attr('src', '/Public/Index/img/s1.png');
            $(this).parent('label').addClass('checked');
            $(this).parents('li').siblings().find('.c-img').attr('src', '/Public/Index/img/s2.png')
            $(this).parents('li').siblings().find('label').removeClass('checked')

            var html = '<span class="one-name"><a>' + name + '</a>';
            html += '<img class="pointer delOperator" data-id="' + id + '" src="/Public/Index/img/close_red.png" alt="close" width="12"/>';
            html += '<input type="hidden" name="user_id" value="' + id + '">';
            html += '<input type="hidden" name="user_name" value="' + name + '"></span>';
            if (parent.$('.operator .main_user span:last').text() == '主办人：') {
                parent.$('.operator .main_user span:last').after(html);
            } else {
                parent.$('.operator .main_user span:last').remove();
                parent.$('.operator .main_user span:last').after(html);
            }
        } else {
            $(this).attr('src', '/Public/Index/img/s2.png')
            $(this).parent('label').removeClass('checked');
            parent.$('.operator .main_user span').each(function () {
                if (name == $(this).text()) {
                    $(this).remove();
                }
            })
        }
    })

    //确定（判断是否选择人员后关闭）
    $('.submitRole').on('click', function () {
        var id = $('input[name="flow_id"]').val();
//        if (id != 0) {
//            if (parent.$('.operator .main_user span:last a').text() != '' || (parent.$('.operator .other_user span:last a').text() != '')) {
//                $('.cancel').click();
//            } else {
//                layer.msg('请选择至少一位用户');
//            }
//        } else {
        closeLayer();
//        }
    })

    //确定部门
    $('.submitDept').on('click', function () {
        closeLayer();
    })

    //提交回退流程
    $('.submitBack').on('click', function () {
        var url = $('.flow2 li').find('input:checked').next().attr('data-url');

        $.get(url, function (res) {
            console.log(res)
            layer.msg = res.info;
            parent.location.href = res.url;
        })
    })

    //选择部门
    $('.all-line').on('click', function () {
        id = $(this).parent('li').attr('data-id');
        name = $(this).text();
        if ($(this).parent('li').attr('class') == 'pointer') {
            $(this).parent('li').addClass('checked');
            var html = '<span><a>' + name + '</a>';
            html += '<img class="pointer delOperator" data-id="' + id + '" src="/Public/Index/img/close_red.png" alt="close" width="12"/>';
            html += '<input type="hidden" name="dept_ids[]" value="' + id + '">';
            html += '<input type="hidden" name="dept_names[]" value="' + name + '"></span>';
            parent.$('.depart').children('span:last').after(html);

        } else {
            $(this).parent('li').removeClass('checked');
            parent.$('.depart span').each(function () {
                if (name == $(this).text()) {
                    $(this).remove();
                }
            })
        }
    })

    //全部添加 部门
    $('.add-all-dept').on('click', function () {
        $('.select_peo ul li').addClass('checked');

        var deptId = new Array();
        var deptName = new Array();
        $('.select_peo ul li').each(function () {
            deptId.push($(this).attr('data-id'));
            deptName.push($(this).children('span').text());
        })

        var html = '<span>';
        for (var i = 0; i < deptId.length; i++) {
            html += '<span><a>' + deptName[i] + '</a>';
            html += '<img class="pointer delOperator" data-id="' + deptId[i] + '" src="/Public/Index/img/close_red.png" alt="close" width="12"/>';
            html += '<input type="hidden" name="dept_ids[]" value="' + deptId[i] + '">';
            html += '<input type="hidden" name="dept_names[]" value="' + deptName[i] + '"></span>';
        }
        html += '</span>';
        parent.$('.depart').html(html);
    })

    //全部删除
    $('.add-del-dept').on('click', function () {
        $('.select_peo ul li').removeClass('checked');
        parent.$('.depart span:not(:first)').remove();
    })

    //选择角色
    $('.all-line-priv').on('click', function () {
        id = $(this).parent('li').attr('data-id');
        name = $(this).text();
        if ($(this).parent('li').attr('class') == 'pointer') {
            $(this).parent('li').addClass('checked');
            var html = '<span><a>' + name + '</a>';
            html += '<img class="pointer delOperator" data-id="' + id + '" src="/Public/Index/img/close_red.png" alt="close" width="12"/>';
            html += '<input type="hidden" name="priv_id[]" value="' + id + '">';
            html += '<input type="hidden" name="priv_name[]" value="' + name + '"></span>';
            parent.$('.priv').children('span:last').after(html);

        } else {
            $(this).parent('li').removeClass('checked');
            parent.$('.priv span').each(function () {
                if (name == $(this).text()) {
                    $(this).remove();
                }
            })
        }
    })

    //全部添加 角色
    $('.add-all-priv').on('click', function () {
        $('.select_peo ul li').addClass('checked');

        var deptId = new Array();
        var deptName = new Array();
        $('.select_peo ul li').each(function () {
            deptId.push($(this).attr('data-id'));
            deptName.push($(this).children('span').text());
        })

        var html = '<span>';
        for (var i = 0; i < deptId.length; i++) {
            html += '<span><a>' + deptName[i] + '</a>';
            html += '<img class="pointer delOperator" data-id="' + deptId[i] + '" src="/Public/Index/img/close_red.png" alt="close" width="12"/>';
            html += '<input type="hidden" name="priv_id[]" value="' + deptId[i] + '">';
            html += '<input type="hidden" name="priv_name[]" value="' + deptName[i] + '"></span>';
        }
        html += '</span>';
        parent.$('.priv').html(html);
    })

    //提交回复日志内容
    $('.submitReply').on('click', function () {
        var url = $(this).attr('data-url');
        var url1 = url.substring(url.lastIndexOf('/'), url.indexOf('/'));
        var url2 = url.substring(url1.lastIndexOf('/'), url1.indexOf('/'));
        var str = url.substring(url.lastIndexOf('/'), url.lastIndexOf('.'));
        var reg = /\d+/g;
        var ms = str.match(reg);
        var newurl = url2 + "/content/" + $(this).parent().siblings('div').find('textarea').val() + '/dia_id/' + ms + '.html';
        if ($(this).parent().siblings('div').find('textarea').val() == '') {
            layer.msg('请填写评论内容');
        } else {
            $.get(newurl, function (res) {
                console.log(res)
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                window.location.reload();

                //            layer.msg = res.info;
                //            parent.location.href = res;
//                parent.location.href=res;
            })
        }
    })

    //计划批注提交
    $('.submitComment').on('click', function () {
        var url = $(this).attr('data-url');
        var data = {id: $('.plan_id').val(), content: $('.feedback').val(), is_end: $('.is_end').val(), dept: $('.dept').val(), cType: $('.cType').val(), lng: $('body').find('.lng').val(), lat: $('body').find('.lat').val()};
        console.log(data);
        var index = layer.load(2, {shade: false});
        $.post(url, data, function (res) {
            console.log(res);
            if (res.status) {
                layer.msg('提交成功', {icon: 6});
                layer.close(index);
                parent.location.reload();
//                setTimeout(function () {
//                    parent.location.href = res.url;
//                }, 1000);
            } else {
                layer.msg('提交失败', {icon: 5});
                layer.close(index);
            }
        })
    })

    //提交周报回复
    $('.submitReplyWeekly').on('click', function () {
        var data = $('form').serialize();
        var url = $(this).attr('data-url');
        var index = layer.load(2, {shade: false});
        $.post(url, data, function (res) {
            if (res.status == 1) {
                layer.msg('回复成功', {icon: 6});
                parent.location.reload();
            } else {
                layer.msg('回复失败', {icon: 5});
                layer.close(index);
            }
            var index2 = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index2);
        })
    })

    //审批不通过
    $('.pass').on('click', function () {
        var value = $(this).val();
        if (value == 3) {
            $('.valid_textarea').show();
        } else {
            $('.valid_textarea').hide();
        }
    })

    //提交审批意思
    $('.submitNotifyVerify').on('click', function () {
        var data = $('form').serialize();
        var url = $(this).attr('data-url');

        var index = layer.load(2, {shade: false});
        $.post(url, data, function (res) {
            console.log(res)
            if (res) {
                parent.location.reload();
            } else {
                layer.msg('提交失败', {icon: 5});
            }
            var index2 = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index2);
        })
    })

    //按月份查询计划
    $('.checkMonth').on('click', function () {
        var month = $(this).attr('data-month');
        var url = $('#monthUrl').val();
        parent.location.href = url + '?month=' + month;
    })

}

function getData(num) {
    url = url + '/p/' + num;
    var id = $('input[name=flow_id]').val();
    // var data = 'dept_id=' + dept_id + '&p=' + num;
    //var url = $('#url').val();
    $.ajax({
        url: url,
        type: 'get',
        typeData: 'json',
        success: function (res) {
            console.log(res);
            var html = '';
            var len = res.list.length;
            if (len > 0) {
                for (var i = 0; i < len; i++) {
                    if (res.list[0].checked && res.list.length == 1) {
                        html += '<li class="pointer" data-id="' + res.list[i].user_id + '">';
                        if (res.list[i].flow_id != 0) {
                            html += '<label class="checked"><img src="/Public/Index/img/s1.png" class="c-img" style="width:12px">主办</label> <span class="c-name checked">' + res.list[i].user_name + '</span></li>';
                        } else {
                            html += '<span class="c-name checked">' + res.list[i].user_name + '</span></li>';
                        }
                    } else if (res.list[0].checked) {
                        html += '<li class="pointer" data-id="' + res.list[i].user_id + '">';
                        if (res.list[i].flow_id != 0) {
                            html += '<label><img src="/Public/Index/img/s2.png" class="c-img" style="width:12px">主办</label> <span class="c-name checked">' + res.list[i].user_name + '</span></li>';
                        } else {
                            html += '<span class="c-name checked">' + res.list[i].user_name + '</span></li>';
                        }
                    } else {
                        html += '<li class="pointer" data-id="' + res.list[i].user_id + '">';
                        if (res.list[i].flow_id != 0) {
                            html += '<label><img src="/Public/Index/img/s2.png" class="c-img" style="width:12px">主办</label> <span class="c-name">' + res.list[i].user_name + '</span></li>';
                        } else {
                            html += '<span class="c-name">' + res.list[i].user_name + '</span></li>';
                        }
                    }

//                html += '<li class="pointer" data-id="' + res.list[i].user_id + '">';
//                if (res.list[i].flow_id != 0) {
//                    html += '<label><img src="/Public/Index/img/s2.png" class="c-img">主办</label>';
//                }
//                html += '<span class="c-name">' + res.list[i].user_name + '</span></li>';
                }
                //插入页码
                var page = '<div>';
                page += '<a class="pointer prev" onclick="getData(' + res.prev + ')" style="width:30px"><<</a>';
                for (var i = 0; i < res.page.length; i++) {
                    if (res.curr == res.page[i]) {
                        page += '<span class="current">' + res.page[i] + '</span>'
                    } else {
                        page += '<a class="pointer num" onclick="getData(' + res.page[i] + ')">' + res.page[i] + '</a>';
                    }
                }
                page += '<a class="pointer next" onclick="getData(' + res.next + ')" style="width:30px">>></a>';
                page += '</div>';
                $('.page').html(page);
            } else {
                html += '<li><span style="color:#bbb">没有符合条件的用户</span></li>';
            }
            $('.select_peo ul').html(html);
        }
    })
}
